package com.graduate.examapp.view.customview

import android.content.Context
import android.content.res.TypedArray
import android.text.InputType
import android.util.AttributeSet
import android.view.View
import android.widget.*
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.MutableLiveData
import com.graduate.examapp.R
import com.graduate.examapp.util.LoginValidator

/**
 * Author: etacao
 * Date: 2023/4/26 11:13
 * Description:
 * History:
 * <author> <time> <version> <desc>
 * etacao 2023/4/26 1.0 首次创建
 */
class CustomEditText : LinearLayout, DefaultLifecycleObserver {
    var editText: EditText? = null
    private var clearView: ImageView? = null
    private var leftDraw: ImageView? = null
    private var wrongHintText: TextView? = null
    private var line: View? = null
    private var visibleView: ImageView? = null
    private var isHidden = true

    var validator: (String) -> Boolean = { false }

    val liveData: MutableLiveData<Boolean> =  MutableLiveData()

    constructor(context: Context): super(context)

    constructor(context: Context, attributeSet: AttributeSet): this(context, attributeSet, 0)

    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int): super(context, attributeSet, defStyleAttr) {
        //将类与补文件相关联、最后一个参数为this 因为要填充到当前容器中
        View.inflate(
            context,
            R.layout.custom_et,
            this
        )
        //获取并设置属性值
        val array: TypedArray = context.obtainStyledAttributes(attributeSet, R.styleable.CustomEditText)
        initElement(array)
        //注意要回收
        array.recycle()
    }

    private fun initElement(array: TypedArray) {
        editText = findViewById(R.id.et)
        clearView = findViewById(R.id.clear_view)
        leftDraw = findViewById(R.id.et_left_draw)
        wrongHintText = findViewById(R.id.tx_wrong_hint)
        line = findViewById(R.id.line)
        visibleView = findViewById(R.id.visible_view)

        array?.let { it ->
            val hint = it.getString(R.styleable.CustomEditText_hintText)
            editText?.hint = hint
            leftDraw?.background = it.getDrawable(R.styleable.CustomEditText_leftDrawSrc)
            wrongHintText?.text = it.getString(R.styleable.CustomEditText_invalidateHint)
            val type = it.getString(R.styleable.CustomEditText_customEditType)
            if (type != "password") {
                //取控件当前的布局参数
                val params = visibleView?.layoutParams as RelativeLayout.LayoutParams
                //设置宽度值
                params.width = 0
                //使设置好的布局参数应用到控件
                visibleView?.layoutParams = params
                //设置校验器
                validator = { id ->
                    LoginValidator.validateId(id)
                }
            } else {
                //设置校验器
                validator = { password ->
                    LoginValidator.validatePassword(password)
                }
                editText?.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                //设置小眼睛 点击事件
                visibleView?.setOnClickListener { it ->
                    val imageView = it as ImageView
                    //开始是隐藏密码 现在点击显示密码
                    if (isHidden) {
                        imageView.setImageResource(R.drawable.icon_visible_gray_20)
                        editText?.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                        isHidden = false
                    } else {
                        //开始是显示密码 现在点击影藏
                        imageView.setImageResource(R.drawable.icon_invisible_gray_20)
                        editText?.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
                        isHidden = true
                    }
                    editText?.let { editText ->
                        editText.setSelection(editText.text.length)
                    }
                }
            }
        }

        editText?.apply {
            setOnFocusChangeListener { view, isFocusOn ->
                val customEditText = view as EditText
                val content = customEditText.text
                if (isFocusOn) {
                    //获取焦点设置 颜色与主题
                    line?.setBackgroundColor(resources.getColor(R.color.master))
                    //提示文字影藏
                    wrongHintText?.visibility = View.GONE
                    if (content.isNullOrBlank()) {
                        clearView?.visibility = View.GONE
                    } else {
                        clearView?.visibility = view.visibility
                    }
                } else {
                    if (content.isNullOrBlank()) {
                        //失去焦点且当前内容为空 设置颜色与主题
                        line?.setBackgroundColor(resources.getColor(R.color.line_not_focus))
                    } else {
                        //失去焦点文本不为空
                        val correct = validator(content.toString())
                        if (correct) {
                            wrongHintText?.visibility = View.GONE
                            line?.setBackgroundColor(resources.getColor(R.color.line_not_focus))
                        } else {
                            wrongHintText?.visibility = View.VISIBLE
                            line?.setBackgroundColor(resources.getColor(R.color.line_hint))
                        }
                    }
                    //一键清除图标 影藏
                    clearView?.visibility = View.GONE
                }
            }
            //监听文本变化
            addTextChangedListener {
                it?.let {
                    val correct = validator(it.toString())
                    //当文本不为空的时候
                    if (it.isNotEmpty()) {
                        // 一键清除图标 显示
                        clearView?.visibility = View.VISIBLE
                        if (correct) {
                            wrongHintText?.visibility = View.GONE
                        }
                    } else {
                        //一键清除图标 影藏
                        clearView?.visibility = View.GONE
                    }
                    liveData.postValue(correct)
                }
            }
            //点击一键清除
            clearView?.setOnClickListener {
                //editText 文本清空
                editText?.text?.clear()
            }
        }
    }
}